package org.oschina.helper;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Component;

/**
 * 线程变量
 */
@Component
public class ThreadVariableHelper {

	private final static ThreadLocal<Map<String,Object>> VARIABLE=new ThreadLocal<Map<String,Object>>();
	
	/**
	 * 设置值
	 * @param key
	 * @param value
	 */
	public void set(String key,Object value)
	{
		getVariable().put(key, value);
	}
	
	/**
	 * 返回
	 * @param key
	 * @return
	 */
	public Object get(String key)
	{
		return getVariable().get(key);
	}
	
	/**
	 * 移除某个
	 * @param key
	 */
	public void remove(String key)
	{
		getVariable().remove(key);
	}
	
	/**
	 * 移除
	 */
	public void remove()
	{
		VARIABLE.remove();
	}
	
	private Map<String,Object> getVariable()
	{
		Map<String,Object> var=VARIABLE.get();
		if(var==null)
		{
			var=new HashMap<String,Object>();
			VARIABLE.set(var);
		}
		return var;
	}
}